import 'package:flutter/cupertino.dart';
import 'package:im_app/im/util/ImConstant.dart';

import '../../../emojiOne/EmojiOneInfoes.dart';
import '../../../pojo/ImPojo.dart';
import '../../../util/FileUtil.dart';
import '../../../util/LoggerUtil.dart';
import '../../../util/MessageUtil.dart';
import '../../../util/Objects.dart';

/// 需要重构，展示用户实际图片
class UserIconView extends StatelessWidget {

  final ImUser user;
  final double iconSize;

  final _logger = createLogger();

  UserIconView({ required this.user, this.iconSize = ImConstant.IM_USER_ICON_SIZE, super.key });

  @override
  Widget build(BuildContext context) {
    var headImg = 'assets/img/head.jpg';
    if (FileUtil.isImage(user.headImg??'')) {
      var imagePath = MessageUtil.getFilePath4(user.headImg??'');
      var isExisted = FileUtil.fileExisted(imagePath);
      if (isExisted) {
        headImg = imagePath;
      } else {
        _logger.e('IMAGE, [path error], ${user.userId}的头像地址有误${user.headImg}', time: DateTime.now());
      }
    } else {
      var emoji = EmojiOneInfoes.getInstance().getByUnicodeOrShortName(user.headImg??'');
      if (Objects.isNotNull(emoji)) {
        headImg = emoji!.emojiPath;
      }
    }
    return ClipRRect(
      borderRadius: BorderRadius.circular(iconSize),
      child: Image(image: AssetImage(headImg), width: iconSize, height: iconSize,),
    );
  }

}